<?php

require_once './include/common.inc.php';
require_once 'left.php';
require_once 'right.php';
include_once './include/cart.class.php';

$page_title = 'Acuity World WebShop';
$menu_id = 0;
$menu_str = PrintMenu();


//https://betaling.wannafind.dk/proxy/p.php/http://webshop.acuityworld.com/new/pay_approved.php?
//transact=1039&orderid=100028&amount=33&currency=208&cardtype=DK&transacknum=1039&actioncode=0

//https://betaling.wannafind.dk/proxy/p.php/http://webshop.acuityworld.com/new/
//pay_approved.php?transact=1034&orderid=TEST100020&amount=40&currency=208&cardtype=DK&transacknum=1034&actioncode=0

$cart_list = '';

$transacknum = $_GET['transacknum'];
$orderid = $_GET['orderid'];
$actioncode = $_GET['actioncode'];
$user_login = true;//used in head.htm

$sql = "select `id` from `cus_order` WHERE `id` = '$orderid' AND `transacknum`=0 limit 1";
$tmp = $db->get_one($sql);


if (!empty($tmp)) {
    // ĶϢ
    $sql = "update `cus_order` set `transacknum` = '$transacknum',`pay_flag` = '0' WHERE `id` = '$orderid' limit 1";
    $db->query($sql);


    $sql = "select `user_id`, `cost`, `cart_info`, `card_name`, `card_number`, `card_type`, `d_street`, `d_city`, `d_state`, `d_zip`, `d_country` from `cus_order` WHERE `id` = '$orderid' limit 1";
    $tmp_order_ary = $db->get_one($sql);

    $user_id = $tmp_order_ary['user_id'];


    // ȡµûϢ
    $sql = "select `email`, `firstname`, `lastname`, `street`, `city`, `state`, `zip`, `country`, `phone`, `mobile` from `cus_members` WHERE `id` = '$user_id' limit 1";
    $user_info_ary = $db->get_one($sql);


    // öûcus_order_tmp
    $sql = "update `cus_order_tmp` set `info` = '' WHERE `user_id` = '$user_id' limit 1";
    $db->query($sql);

    // ===================== Ӷ start =====================================
    $cart = new cart();
    if (!$cart = unserialize($tmp_order_ary['cart_info'])) {
        $cart = new cart();
    }
		/////update stock_data--------------------------------------leo
		$myCart = $cart->getData();
		$sql_update="";
		foreach($myCart as $key=>$val)
		{
			if($key!="")
			{
				if($val["count"]>0)
				{
					$sql_update="update pro_product set stock_data=stock_data-".$val["count"]." where id=".$key;
					$mysql->query($sql_update);
				}
			}
		}
	
		///////////////

    $promoter_id = $cart->getPromoterId();

    if ($promoter_id > 0) {
        $sql = "select `rank`, `faq1`, `firstname`, `lastname` from `promoter_info` WHERE `id` = '$promoter_id' AND `status` = 1 AND `rank`>0 limit 1";
        $tmp_ary = $db->get_one($sql);
        if (!empty($tmp_ary)) {

            $commission = 0;   // Ҫ֧Ӷ

            // ȡCommission rates
            $rank_str = 'commission_rates_' . strtolower($RANK_ARY[$tmp_ary['rank']]);
            $c_r_str = getMvalue($rank_str);

            /*
            * $c_r_products     // Commission rates: Products
            * $c_r_special      // Commission rates: Special Products
            * $c_r_trainings    // Commission rates: Trainings
            * $c_r_pcsd1        // Commission rates: Promoter Commission Settlement Dates 1
            * $c_r_pcsd2        // Commission rates: Promoter Commission Settlement Dates 2
            */

            list($c_r_products, $c_r_special, $c_r_trainings, $c_r_pcsd1, $c_r_pcsd2) = explode("||", $c_r_str);

            $cart_info_ary = $cart->getData();

            foreach ($cart_info_ary as $k=>$v) {
                if ($v['type'] == 'Courses / Programs') {
                    $commission += $v['cost2']*$c_r_trainings/100;
                } elseif ($v['productinfo']['is_special'] == 1) {
                    $commission += $v['cost2']*$c_r_special/100;
                } else {
                    $commission += $v['cost2']*$c_r_products/100;
                }

               $cart_list .= '<tr>
                        <td class="en_black_12">'. $v['name'] .'</td>
                        <td class="en_black_12">'. $v['old_price'] .'</td>
                        <td class="en_black_12">'. $v['count'] .'</td>
                        <td class="en_black_12">'. $v['cost'] .'</td>
                      </tr>';
            }

            $commission = number_format($commission, 2, '.', '');

            $sql = "INSERT INTO `promoter_commission` (`promoter_id`, `order_id`, `members_id`, `cost`, `commission`, `status`, `date`) VALUES
            ('$promoter_id', '$orderid', '$user_id', '" . $tmp_order_ary['cost'] . "', '$commission', 0, '$timestamp')";
            $db->query($sql);
        }

    }
	else
	{
		$cart_info_ary = $cart->getData();
		foreach ($cart_info_ary as $k=>$v) 
		{
			$cart_list .= '<tr>
					<td class="en_black_12">'. $v['name'] .'</td>
					<td class="en_black_12">'. $v['old_price'].'</td>
					<td class="en_black_12">'. $v['count'] .'</td>
					<td class="en_black_12">'. $v['cost'] .'</td>
				  </tr>';
		}
		
	}
	$totalCost=$cart->getTotalCost();
	$discount=$cart->getDisCount();
	$new_total=number_format($totalCost-$discount,2,'.','');
	$vat_fee=$cart->getAddVat();
	$addshippingfee=$cart->getAddShippingFee();
	$totalCost2=$cart->getTotalCost2();
	
	$cart_list .= '<tr>
					<td class="en_black_12" colspan="4" align="right">
						<strong>Sub Total:</strong> DKK'.$totalCost.'<br/> 
						<strong>Discount:</strong> DKK'.$discount.'<br/>
						<strong>New Sub Total:</strong> DKK'.$new_total.'<br/> 
						<strong>VAT:</strong> DKK'.$vat_fee.'<br/> 
						<strong>Shipping Fee:</strong> DKK'.$addshippingfee.'<br/> 
						<strong>Total:</strong> '.$totalCost2.'</td>				
				  </tr>';
    // ===================== Ӷ end =======================================

    
    // ʼ send_email
    $username = empty($user_info_ary['lastname']) ? $user_info_ary['firstname'] : $user_info_ary['firstname'] . ' ' . $user_info_ary['lastname'];
    $email = $user_info_ary['email'];
    $day = date("Y-m-d", $timestamp);
    $deliveryaddress = $tmp_order_ary['d_street'] . "<br>" . $tmp_order_ary['d_city'] . "<br>" . $tmp_order_ary['d_state'] . "<br>" . $tmp_order_ary['d_zip'] . "<br>" . $tmp_order_ary['d_country'];
    $customaddress = $user_info_ary['street'] . "<br>" . $user_info_ary['city'] . "<br>" . $user_info_ary['state'] . "<br>" . $user_info_ary['zip'] . "<br>" . $user_info_ary['country'];
    $promoter_name = empty($tmp_ary['lastname']) ? $tmp_ary['firstname'] : $tmp_ary['firstname'] . ' ' . $tmp_ary['lastname'];
    $phone = str_replace("||", " " , $user_info_ary['phone']);
    $mobile = str_replace("||", " " , $user_info_ary['mobile']);
	$tmp_order_ary['card_number']=substr($tmp_order_ary['card_number'], -4); 

    require_once './mail/order_to_members.php';
    sendhtmlmail($from_name,$from_email,$to_name,$to_email,$subject,$content);   

    require_once './mail/order_to_webmaster.php';
    sendhtmlmail($from_name,$from_email,$to_name,$to_email,$subject,$content);
    
}


require_once PrintEot('header');
require_once PrintEot('left');
require_once PrintEot('pay_approved');
require_once PrintEot('right');
require_once PrintEot('footer');



?>